package com.github.pig.admin.controller.app;
import java.util.List;
import java.util.Map;
import java.util.Date;

import com.github.pig.admin.controller.SysCommunityUserController;
import com.github.pig.admin.model.app.AppMessageShow;
import com.github.pig.admin.model.zhgl.community.CommunityInfo;
import com.github.pig.admin.service.app.IAppMessageShowService;
import com.github.pig.admin.service.sys.SysCommunityUserService;
import com.github.pig.admin.service.sys.SysUserService;
import com.github.pig.admin.service.zhgl.community.ICommunityInfoService;
import com.github.pig.common.vo.UserVO;
import io.jsonwebtoken.Claims;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.pig.common.constant.CommonConstant;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.github.pig.common.util.R;
import com.github.pig.common.web.BaseController;

/**
 * <p>
 * app展示信息基础表 前端控制器
 * </p>
 *
 * @author xiaosa
 * @since 2018-07-09
 */
@RestController
@RequestMapping("/appMessageShow")
public class AppMessageShowController extends BaseController {
    @Autowired
    private IAppMessageShowService appMessageShowService;
    @Autowired
    private ICommunityInfoService communityInfoService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysCommunityUserService sysCommunityUserService;
    @Autowired
    private SysCommunityUserController sysCommunityUserController;

    /**
     * 通过ID查询
     *
     * @param id ID
     * @return AppMessageShow
     */
    @GetMapping("/{id}")
    public R<AppMessageShow> get(@PathVariable Integer id) {
        return new R<>(appMessageShowService.selectById(id));
    }


    /**
     * 分页查询信息
     *
     * @param params 分页对象
     * @return 分页对象
     */
    @RequestMapping("/page")
    public Page page(@RequestParam Map<String, Object> params,AppMessageShow appMessageShow) {
        EntityWrapper entityWrapper = new EntityWrapper<AppMessageShow>();
        Page<AppMessageShow> page = new Page<AppMessageShow>(Integer.parseInt(params.getOrDefault("page", 1).toString())
                , Integer.parseInt(params.getOrDefault("limit", 10).toString()));
        entityWrapper.setSqlSelect("id AS id,type,title,community_id AS communityId,create_by AS createBy,create_date AS createDate,update_by AS updateBy,update_by AS updateBy,update_date AS updateDate,del_flag AS delFlag,(SELECT c.name FROM community_info c WHERE c.id = community_id) AS communityName ");
        entityWrapper.eq("del_flag", CommonConstant.STATUS_NORMAL);

        // 获取到当前登录用户
        Claims claims = getCurrentUserInfo();
        String currentUserName =  (String) claims.get("user_name");
        UserVO userVO = sysUserService.findUserByUsername(currentUserName);

        List communityIdList = sysCommunityUserController.getCurrentUserCommunityId(userVO);

        // 判断是否是超级管理员"admin"
        if(CommonConstant.MANAGE_NAME.equalsIgnoreCase(currentUserName) == false){

            if (communityIdList != null && communityIdList.size() != 0){

                // 如果获取到的所属小区id集合不为空
                entityWrapper.in("community_id",communityIdList);
            }
        }
        if(StringUtils.isNotEmpty(appMessageShow.getTitle())){
            entityWrapper.like("title",appMessageShow.getTitle());
        }
        if(StringUtils.isNotEmpty(appMessageShow.getCommunityId())){
            entityWrapper.eq("community_id",appMessageShow.getCommunityId());
        }
        return appMessageShowService.selectPage(page,entityWrapper);
    }

    /**
     * 添加
     * @param  appMessageShow  实体
     * @return success/false
     */
    @RequestMapping("/add")
    public R<Boolean> add(@RequestBody AppMessageShow appMessageShow) {
        R<Boolean> r = new R();
        r.setData(true);
        // 根据小区id获取到小区名称
        CommunityInfo communityInfo = communityInfoService.selectById(appMessageShow.getCommunityId());
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.eq("community_id",appMessageShow.getCommunityId());
        entityWrapper.eq("del_flag", CommonConstant.STATUS_NORMAL);
        List result = appMessageShowService.selectList(entityWrapper);
        if(result!=null&&result.size()>0){
            r.setMsg("已经维护该小区轮播图，可直接进行修改");
            r.setData(false);
        }else{
            // 获取到小区名称并存入实体中
            appMessageShow.setCommunityName(communityInfo.getName());
            appMessageShowService.insert(appMessageShow);
        }

        return r;
    }

    /**
     * 删除
     * @param id ID
     * @return success/false
     */
    @RequestMapping("/delete")
    public R<Boolean> delete(String id) {
        AppMessageShow appMessageShow = new AppMessageShow();
        appMessageShow.setId(id);
        appMessageShow.setUpdateDate(new Date());
        appMessageShow.setDelFlag(CommonConstant.STATUS_DEL);
        return new R<>(appMessageShowService.updateById(appMessageShow));
    }

    /**
     * 编辑
     * @param  appMessageShow  实体
     * @return success/false
     */
    @RequestMapping("/edit")
    public R<Boolean> edit(@RequestBody AppMessageShow appMessageShow) {
        R<Boolean> r = new R();
        r.setData(true);
        appMessageShow.setUpdateDate(new Date());
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.eq("community_id",appMessageShow.getCommunityId());
        entityWrapper.eq("del_flag", CommonConstant.STATUS_NORMAL);
        entityWrapper.ne("id",appMessageShow.getId());
        List result = appMessageShowService.selectList(entityWrapper);
        if(result!=null&&result.size()>0){
            r.setMsg("已经维护该小区轮播图，可直接进行修改");
            r.setData(false);
        }
        // 根据小区id获取到小区名称
        CommunityInfo communityInfo = communityInfoService.selectById(appMessageShow.getCommunityId());
        // 获取到小区名称并存入实体中
        appMessageShow.setCommunityName(communityInfo.getName());
        appMessageShowService.updateById(appMessageShow);
        return r;
    }
}
